'use strict';

const conn = require('../../libs/Conn');
const _ = require('underscore');

module.exports = async function (params) {
    /*分页*/
    let pageSql = `LIMIT ${params.pageNum>1?((params.pageNum-1)*10):0},10;SELECT FOUND_ROWS() AS count;`;
    /*排序*/
    let orderBySql = ``;
    if(params.sort&&params.dir){
        switch (params.sort) {
            case 'progress_no_no':
                orderBySql = `ORDER BY epid.progress_no ${params.dir}`;
                break;
            case 'supervision_date':
                orderBySql = `ORDER BY epid.supervision_date ${params.dir}`;
                break;
            case 'supervisor_name':
                orderBySql = `ORDER BY ufb.user_name${params.dir}`;
                break;
            case 'inspection_date':
                orderBySql = `ORDER BY epid.inspection_date ${params.dir}`;
                break;
            case 'inspection_name':
                orderBySql = `ORDER BY epid.inspection_name ${params.dir}`;
                break;
            case 'reporter_name':
                orderBySql = `ORDER BY ufa.user_name ${params.dir}`;
                break;
            case 'inspection_result':
                orderBySql = `ORDER BY  epid.inspection_result ${params.dir}`;
                break;
        }
    }
    let querySql = `SELECT
                        SQL_CALC_FOUND_ROWS
                        epid.engineering_id,
                        epid.information_id,
                        epid.progress_no,
                        DATE_FORMAT(
                            epid.supervision_date,
                            '%Y-%m-%d'
                        ) AS supervision_date,
                        epid.reporter_name,
                        epid.reporter_phone AS mobile,
                        DATE_FORMAT(
                            epid.inspection_date,
                            '%Y-%m-%d'
                        ) AS inspection_date,
                        epid.inspection_name,
                        epid.inspection_result,
                        epid.rectification_notice,
                        epid.rectification_reply,
                         DATE_FORMAT(
                            epid.rectification_date,
                            '%Y-%m-%d'
                        ) AS rectification_date,
                        epid.supervision_details,
                        epid.remarks,
                        ufb.user_name AS supervisor_name,
                        ufb.user_id AS supervisor_id
                    FROM
                        engineering_progress_information_data epid
                    LEFT JOIN progress_information_user_middle pium ON pium.information_id = epid.information_id
                    LEFT JOIN user_file ufb ON ufb.user_id = pium.user_id
                    AND ufb.delete_sign = 1
                    WHERE
                        epid.engineering_id = '${params.engineering_id}'
                    AND epid.progress_type = '${params.progress_type}'
                    AND epid.delete_sign = 1 ${orderBySql} ${pageSql}`;
    let rec = await conn(querySql);
    let dataList = [];
    let queryData=_.groupBy(rec[0],'information_id');
    for(let group in queryData){
        let name = '';
        let userIdList = [];
        let groupList = queryData[group];
        for(let data in groupList){
            if(name ==='null'){
               continue;
            }else{
                name+= groupList[data].supervisor_name+',';
                userIdList.push(groupList[data].supervisor_id)
            }
        }
        name = name.slice(0,name.length-1);
        if(groupList[0].supervision_date === '0000-00-00' || groupList[0].supervision_date === null){
            groupList[0].supervision_date = '';
        }
        groupList[0].supervisor_id=userIdList;
        groupList[0].inspection =  groupList[0].inspection_name;
        groupList[0].user_information =  groupList[0].reporter_name + '-'+ (groupList[0].mobile === null?'未留电话':groupList[0].mobile);
        switch (groupList[0].inspection_result) {
            case 1:
                groupList[0].inspection_result_text = '是';
                break;
            case 2:
                groupList[0].inspection_result_text = '否';
                break;
        }
        dataList.push(groupList[0])
    }
    let pageNum = Math.ceil(rec[1][0].count/10);
    if(dataList.length>0){
        dataList[0].dataTotal = rec[1][0].count;
        dataList[0].pageTotal = pageNum;
        dataList[0].pageNum = params.pageNum;
    }
    return {success: true, data: dataList};

};